home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / avres.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-31  |  3.8 KB  |  98 lines

  1. // avres.cpp
  2. //
  3. // Example program show use of the class "CmReserve".  This program
  4. // prompts for names and test scores (between 0 and 100) until an "x"
  5. // (exit) is typed for a name.  The scores are then printed to the
  6. // screen along with the average score.  Both the name list and the
  7. // score list are then written to a binary file using the reserve's
  8. // "write" functions.  All subsequent runs of this program start by
  9. // first reading this file using the reserve's "read" function.  The
  10. // name list and score list are printed to the screen and then the
  11. // user is prompted for names and scores to add to the existing list.
  12. //
  13. #include <stdio.h>
  14. #include <cm/include/cmstring.h>
  15. #include <cm/include/cmreserv.h>
  16. #include "integer.h"
  17.  
  18. const char*   RESERVENAME = "AVRES.DAT";         // Reserve file name.
  19. const char*   RESERVEID   = "RESERVE EXAMPLE";   // Reserve identifier.
  20. const Integer min(0);                            // Minimum score value.
  21. const Integer max(100);                          // Maximum score value.
  22.  
  23. void printReserve();                             // Reserve output func.
  24.  
  25. CmContainer *nameRoot, *scorRoot;                // Root container ptrs.
  26.  
  27. int main()                                       // Start main.
  28. {
  29.   CmReserve::initialize   ();                    // Register Cm classes.
  30.   CmReserve::registerClass(new Integer);         // Register integer class.
  31.  
  32.   CmReserve reserve(RESERVENAME);                // Declare the reserve.
  33.   reserve.identifier(RESERVEID);                 // Set the identifier.
  34.  
  35.   if (reserve.exists() && reserve.read())        // Read the reserve.
  36.   {
  37.     nameRoot = reserve["NAMES"];                 // Get names root.
  38.     scorRoot = reserve["SCORES"];                // Get scores root.
  39.     printReserve();                              // Print the reserve.
  40.   }
  41.   else                                           // Reserve is new.
  42.   {
  43.     nameRoot = reserve.createRoot("NAMES");      // Create names root.
  44.     scorRoot = reserve.createRoot("SCORES");     // Create scores root.
  45.   }
  46.  
  47.   Bool     done = FALSE;
  48.   CmString name, cscore;
  49.   int      score;
  50.  
  51.   while (!done)                                  // While getting input,
  52.   {
  53.     cout << "Enter name (\"x\" when done): " << flush; // Read name.
  54.     cin  >> name;
  55.     if (name[0] == 'x')                          // 'x' = time to exit.
  56.     {
  57.       printReserve();                            // Print the reserve.
  58.       reserve.write();                           // Write the reserve.
  59.       done = TRUE;                               // Finished.
  60.     }
  61.  
  62.     else                                         // Got a name.
  63.     {
  64.       cout << "Enter score               : " << flush; // Read the score.
  65.       cin  >> cscore;
  66.       sscanf((const char*) cscore, "%d", &score);
  67.  
  68.       if (score < (int) min || score > (int) max)  // Out of range.
  69.         cout << "Score must be between " << min << "and " << max << endl;
  70.       else
  71.       {
  72.         nameRoot->add(new CmString(name));       // Add name to root.
  73.         scorRoot->add(new Integer(score));       // Add score to root.
  74.       }
  75.     }
  76.   }
  77.   return 0;
  78. }
  79.  
  80. void printReserve()                              // Reserve print function.
  81. {
  82.   cout << endl << "Reserve contents (name, score):" << endl;
  83.   CmIterator *iterator1 = nameRoot->newIterator();  // Use "newIterator"
  84.   CmIterator *iterator2 = scorRoot->newIterator();
  85.   int         average   = 0;
  86.   int         total     = 0;
  87.   while (!iterator1->done() && !iterator2->done())
  88.   {
  89.     Integer* pInt = (Integer*) iterator2->next();
  90.     cout << *(iterator1->next()) << " - " << *pInt << endl;
  91.     average += (int) (*pInt); total++;
  92.   }
  93.   cout << "--------------------" << endl;
  94.   cout << "Average score is " << average / total << "." << endl;
  95.   delete iterator1;          // Must delete the iterators.
  96.   delete iterator2;
  97. }
  98.